package com.xmhmyh.generalframework.common;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.google.common.collect.Lists;
import com.xmhmyh.generalframework.common.constants.Constants;
import com.xmhmyh.generalframework.common.entity.Condition;
import com.xmhmyh.generalframework.common.entity.DataGridRequest;
import com.xmhmyh.generalframework.common.enums.Operator;
import com.xmhmyh.generalframework.common.utils.ServletUtils;
import com.xmhmyh.generalframework.framework.redis.IredisService;
import com.xmhmyh.generalframework.framework.security.LoginUser;
import com.xmhmyh.generalframework.framework.security.TokenService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 于丁
 * @DateTime: 2020/7/21 4:02 下午
 */
public class BaseController {
    @Autowired
    TokenService tokenService;
    @Autowired
    protected IredisService iredisService;

    /**
     * @Author: 于丁
     * @Description: 获取LoginUser
     * @DateTime: 2020/7/24 10:26 上午
     * @Params:
     * @Return
     */
    public LoginUser getLoginUser() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return loginUser;
    }

    /**
     * @Author: 于丁
     * @Description: 添加账户约束
     * @DateTime: 2020/7/29 7:46 下午
     * @Params:
     * @Return
     */
    public DataGridRequest addShopAccountConstraints(DataGridRequest dataGridRequest) {
        LoginUser loginUser = getLoginUser();
        ArrayList<String> values = Lists.newArrayList();
        values.add(String.valueOf(loginUser.getUser().getAccountId()));
        dataGridRequest.getConditions().add(new Condition("accountId", Operator.EQ.getLabel(), values));
        return dataGridRequest;
    }
    /**
     * @Author: 于丁
     * @Description: 添加账户约束
     * @DateTime: 2020/7/29 7:46 下午
     * @Params:
     * @Return
     */
    public DataGridRequest addAccountConstraints(DataGridRequest dataGridRequest) {
        LoginUser loginUser = getLoginUser();
        ArrayList<String> values = Lists.newArrayList();
        values.add(String.valueOf(loginUser.getUser().getAccountId()));
        dataGridRequest.getConditions().add(new Condition("accountId", Operator.EQ.getLabel(), values));
        return dataGridRequest;
    }
    /**
     * @Author: 于丁
     * @Description: 添加账户约束
     * @DateTime: 2020/7/29 7:46 下午
     * @Params:
     * @Return
     */
    public DataGridRequest addAccountConstraintsAndSuper(DataGridRequest dataGridRequest) {
        LoginUser loginUser = getLoginUser();
        ArrayList<String> values = Lists.newArrayList();
        values.add(String.valueOf(loginUser.getUser().getAccountId()));
        values.add(String.valueOf(Constants.SUPER_ACCOUNT_ID));
        dataGridRequest.getConditions().add(new Condition("accountId", Operator.IN.getLabel(), values));
        return dataGridRequest;
    }

    protected void exportExcel(HttpServletResponse resp, List<List<String>> head, List<List<Object>> rows, String name) {
        ServletOutputStream outputStream = null;
        try {
            outputStream = resp.getOutputStream();
            String fileName = name + ".xlsx";
            EasyExcel.write(outputStream).head(head).sheet(fileName).doWrite(rows);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/vnd.ms-excel");
            resp.setHeader("content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ExcelTypeEnum.XLSX.getValue(), "utf-8"));
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
